/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cjf.kotlinaopdemo

import android.Manifest
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.cjf.aop.annotation.*
import com.cjf.aop.enums.ThreadType
import com.cjf.aop.runtime.AOPManager
import com.cjf.aop.runtime.logger.XLogger
import com.cjf.aop.runtime.utils.AppExecutors

class AppDemoMainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var mTvHello: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mTvHello = findViewById(R.id.tv_hello)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btn_single_click -> handleOnClick(v)
            R.id.btn_request_permission -> handleRequestPermission(v)
            R.id.btn_main_thread -> {
                AppExecutors.get().poolIO().execute {
                    val s = doInMainThread(v)
                    XLogger.e("子线程内打印主线程返回结果：$s")
                }
            }
            R.id.btn_io_thread ->  {
                val s = doInIOThread(v)
                mTvHello.text = s
            }
            R.id.btn_try_catch -> {
                mTvHello.text = resources.getString(R.string.result_is, getNumber().toString())
            }
            R.id.btn_lambda -> {}
            else -> {
            }
        }
    }

    @ApplyPermission(
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.RECORD_AUDIO)
    fun handleRequestPermission(view: View) {
        Toast.makeText(AOPManager.getContext(), "权限已申请", Toast.LENGTH_SHORT).show();
    }

    @Intercept(MyApplication.INTERCEPT_LOGIN)
    private fun doSomeThing() {
        XLogger.e("已经登录了！")
    }


    @CaptureThrowable(MyApplication.TRY_CATCH_KEY)
    private fun getNumber(): Int {
        return 100 / 0
    }


    @SingleClick
    @DebugLog(priority = Log.ERROR)
    private fun handleOnClick(v: View) {
        XLogger.e("点击响应！")
        //Toast.makeText(AppDemoMainActivity.this, "你点击了", Toast.LENGTH_SHORT).show()
    }

    @WorkThread(ThreadType.MAIN)
    private fun doInMainThread(v: View): String {
        mTvHello.text = "工作在主线程"
        return "工作在主线程"
    }

    @WorkThread(ThreadType.IO_Single)
    private fun doInIOThread(v: View): String {
        return "io线程名:" + Thread.currentThread().name
    }

    companion object {
        const val TRY_CATCH_KEY = "getNumber"
    }
}